package hospital.daoImpl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import hospital.dao.AdminDao;
import hospital.model.EmpInfoModel;
import hospital.model.LoginModel;
import hospital.model.OfficeInfoModel;
import hospital.model.PatientInfoModel;
import hospital.model.UserInfoModel;
import hospital.util.HospitalUtil;

public class AdminDaoImpl implements AdminDao {
	private static final Logger log = Logger.getLogger(StaffDaoImpl.class);
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public boolean selectCheckAvailability(String email) {
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(LoginModel.class);
		cr.add(Restrictions.eq("username", email));
		try {
			if (cr.uniqueResult() == null) {
				return true;
			} else {
				return false;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean selectCheckSinUnique(String sin) {
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(UserInfoModel.class);
		cr.add(Restrictions.eq("sin", sin));
		try {
			if (cr.uniqueResult() == null) {
				return true;
			} else {
				return false;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean selectCheckOffice(String officePhone) {
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(OfficeInfoModel.class);
		cr.add(Restrictions.eq("officePhone", officePhone));
		try {
			if (cr.uniqueResult() != null) {
				return true;
			} else {
				return false;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public OfficeInfoModel selectGetOfficeInfo(String officePhone) {
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(OfficeInfoModel.class);
		cr.add(Restrictions.eq("officePhone", officePhone));
		try {
			return (OfficeInfoModel) cr.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean insertCreateEmp(LoginModel loginInfo, EmpInfoModel empInfo) {
		try {
			sessionFactory.getCurrentSession().save(loginInfo);
			sessionFactory.getCurrentSession().save(empInfo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List<UserInfoModel> selectGetUserResult(long userId, String firstname, String lastname) {
		Criteria cr = sessionFactory.getCurrentSession().createCriteria(UserInfoModel.class);
		if (userId != -1) {
			cr.add(Restrictions.eq("userId", userId));
		}
		if (!firstname.equals("")) {
			cr.add(Restrictions.eq("firstname", firstname));
		}
		if (!lastname.equals("")) {
			cr.add(Restrictions.eq("lastname", lastname));
		}
		cr.addOrder(Order.asc("userId"));
		try {
			return (List<UserInfoModel>) cr.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean updateResetPassword(long userId) {
		try {
			Session session = sessionFactory.getCurrentSession();
			Criteria cr = session.createCriteria(UserInfoModel.class);
			cr.add(Restrictions.eq("userId", userId));

			if (cr.uniqueResult() != null) {
				UserInfoModel userInfo = (UserInfoModel) cr.uniqueResult();
				LoginModel login = (LoginModel) session.get(LoginModel.class, userInfo.getEmail());
				login.setPassword(userInfo.getSin());
				session.update(login);
				return true;
			} else {
				return false;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			return false;
		}
	}

}
